<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%>
<%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c" %>
<%
String path = request.getContextPath(); String basePath =
request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";
%>
<%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c"%>
<!DOCTYPE html>
<html>

	<head>
		<base href="<%=basePath%>">
		<meta charset="utf-8">
		<meta name="viewport" content="width=device-width, initial-scale=1.0,maximum-scale=1.0, user-scalable=0">

		<title>地址管理</title>

		<link href="css/admin.css" rel="stylesheet" type="text/css">
		<link href="css/amazeui.css" rel="stylesheet" type="text/css">

		<link href="css/personals.css" rel="stylesheet" type="text/css">
		<link href="css/addstyle.css" rel="stylesheet" type="text/css">
		<script src="js/jquery.min.js" type="text/javascript"></script>
		<script src="js/amazeui.js"></script>
	</head>

	<body>
		<!--头 -->
		<jsp:include page="common/header.jsp"/>
		<input type="hidden" value="${user.userid }" id="userid"/>
		<b class="line"></b>

		<div class="center">
			<div class="col-main">
				<jsp:include page="common/user_left.jsp"></jsp:include>
				<div class="main-wrap">
					<div class="user-address">
						<!--标题 -->
						<div class="am-cf am-padding">
							<div class="am-fl am-cf"><strong class="am-text-danger am-text-lg">地址管理</strong> / <small>Address&nbsp;list</small></div>
						</div>
						<hr/>
						<ul class="am-avg-sm-1 am-avg-md-3 am-thumbnails">
							<c:forEach items="${address }" var="as">
							<li class="user-addresslist <c:if test="${as.isdefault==0 }">defaultAddr</c:if> ">
								<span class="new-option-r" onclick="setdefault(${as.addressid});"><i class="am-icon-check-circle"></i>默认地址</span>
								<p class="new-tit new-p-re">
									<span class="new-txt">${as.username }</span>
									<span class="new-txt-rd2">${as.userphone }</span>
								</p>
								<div class="new-mu_l2a new-p-re">
									<p class="new-mu_l2cw">
										<span class="title">${as.address }</span>
									</p>
								</div>
								<div class="new-addr-btn">
									<a href="javascript:void();" onclick="selAddress(${as.addressid});"><i class="am-icon-edit"></i>编辑</a>
									<span class="new-addr-bar">|</span>
									<a href="javascript:delAddress(${as.addressid });" id="delbtn" ><i class="am-icon-trash"></i>删除</a>
								</div>
							</li>
							</c:forEach>
						</ul>
						<script type="text/javascript">
							var userid=$("#userid").val();
							function selAddress(aid){
								$.ajax({
					    	        type:"POST",
					    	        url:'/address/addressInfo?aid='+aid,
					    	        data:"",
					    	        dataType:"json",
					    	        error: function(request) {
					    	            alert("Connection error");
					    	        },
					    	        success:function(data) {
					    	        	$("#user-name").val(data.username);
					    	        	$("#user-phone").val(data.userphone);
					    	        	$("#user-address").val(data.address);
					    	        	$("#user-code").val(data.postcode);
					    	        	$("#addressid").val(data.addressid);
					    	        	$("#addbtn").attr("onclick","updAddress()");
					    	        }
					    	    });
							}
							function updAddress(){
								$.ajax({
					    	        cache:true,
					    	        type: "POST",
					    	        url:'/address/updAddress',
					    	        data:$('#addressfrm').serialize(),// 你的formid
					    	        async: false,
					    	        dataType:"json",
					    	        error: function(request) {
					    	            alert("Connection error");
					    	        },
					    	        success:function(data) {
					    	        	if(data){
					    	        		layer.msg('修改地址信息成功！',{icon:1,time:1000});
											setTimeout(function(){//两秒后跳转  
						                       location.href="/address/getaddress/"+userid;
						                    },1000);
					    	        	}else{
					    	        		layer.msg('修改信息异常请稍后重试！',{icon:2,time:1000});
					    	        	}
					    	        }
					    	    });
							}
							
							function addAddress(){
								$.ajax({
					    	        cache:true,
					    	        type:"POST",
					    	        url:'/address/addAddress',
					    	        data:$('#addressfrm').serialize(),// 你的formid
					    	        async: false,
					    	        dataType:"json",
					    	        error: function(request) {
					    	            alert("Connection error");
					    	        },
					    	        success:function(data) {
					    	        	if(data){
					    	        		layer.msg('添加地址信息成功！',{icon:1,time:1000});
											setTimeout(function(){//两秒后跳转  
						                       location.href="/address/getaddress/"+userid;
						                    },1000);
					    	        	}else{
					    	        		layer.msg('添加信息异常请稍后重试！',{icon:2,time:1000});
					    	        	}
					    	        }
					    	    });
							}
							
							function delAddress(aid){
								$.ajax({
					    	        type:"POST",
					    	        url:'/address/delAddress/'+aid,
					    	        data:"",
					    	        dataType:"json",
					    	        error: function(data) {
					    	            alert("Connection error");
					    	        },
					    	        success:function(data) {
					    	        	if(data){
					    	        		layer.msg('删除地址信息成功！',{icon:1,time:1000});
											setTimeout(function(){//两秒后跳转  
						                       location.href="/address/getaddress/"+userid;
						                    },1000);
					    	        	}else{
					    	        		layer.msg('删除信息异常请稍后重试！',{icon:2,time:1000});
					    	        	}
					    	        }
					    	    });
							}
							
							function setdefault(sid){
								$.ajax({
					    	        type:"POST",
					    	        url:'/address/setDefaultArs/'+sid,
					    	        data:"",
					    	        dataType:"json",
					    	        error: function(data) {
					    	            alert("Connection error");
					    	        },
					    	        success:function(data) {
					    	        }
					    	    });
							}
						</script>
						<div class="clear"></div>
						<a class="new-abtn-type" data-am-modal="{target: '#doc-modal-1', closeViaDimmer: 0}">添加新地址</a>
						<!--例子-->
						<div class="am-modal am-modal-no-btn" id="doc-modal-1">

							<div class="add-dress">

								<!--标题 -->
								<div class="am-cf am-padding">
									<div class="am-fl am-cf"><strong class="am-text-danger am-text-lg">新增地址</strong> / <small>Add&nbsp;address</small></div>
								</div>
								<hr/>

								<div class="am-u-md-12 am-u-lg-8" style="margin-top: 20px;">
									<form class="am-form am-form-horizontal" id="addressfrm">
										<input type="hidden" id="addressid" name="addressid" value="0"/>
										<input type="hidden" name="userid" value="${user.userid }"/>
										<div class="am-form-group">
											<label for="user-name" class="am-form-label">收货人</label>
											<div class="am-form-content">
												<input type="text" id="user-name" name="username" placeholder="收货人">
											</div>
										</div>

										<div class="am-form-group">
											<label for="user-phone" class="am-form-label">手机号码</label>
											<div class="am-form-content">
												<input id="user-phone" name="userphone" placeholder="手机号必填" type="tel">
											</div>
										</div>
										<div class="am-form-group">
											<label for="user-address" class="am-form-label">收货地址</label>
											<div class="am-form-content address">
												<input type="text" id="user-address" name="address" placeholder="收货地址"/>
											</div>
										</div>
										<div class="am-form-group">
											<label for="user-address" class="am-form-label">邮编</label>
											<div class="am-form-content address">
												<input type="text" id="user-code" name="postcode" placeholder="邮编"/>
											</div>
										</div>


										<div class="am-form-group">
											<div class="am-u-sm-9 am-u-sm-push-3">
												<input class="am-btn am-btn-danger" type="button" id="addbtn" onclick="addAddress();" value="保存"/>
												<a href="javascript:;" class="am-close am-btn am-btn-danger" data-am-modal-close>取消</a>
											</div>
										</div>
									</form>
								</div>

							</div>

						</div>

					</div>

					<script type="text/javascript">
					///address/updAddress
						$(document).ready(function() {							
							$(".new-option-r").click(function() {
								$(this).parent('.user-addresslist').addClass("defaultAddr").siblings().removeClass("defaultAddr");
							});
							
							var $ww = $(window).width();
							if($ww>640) {
								$("#doc-modal-1").removeClass("am-modal am-modal-no-btn")
							}
							
						})
					</script>

					<div class="clear"></div>

				</div>
				<!--底部-->
				<jsp:include page="common/footer.jsp"></jsp:include>
			</div>
		</div>
	</body>

</html>